package com.demo.view;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.appcompat.widget.AppCompatButton;
import com.tg.appcommon.android.PermissionUtil;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGLog;

/**
 * Describe:
 * Created by Rance Rang on 2019-11-26.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */
public class RecordAudioView extends AppCompatButton {
    public static final String FROM_LOCKBELL = "from_lock_bell";
    private IRecordAudioListener recordAudioListener;
    private boolean isRecording;
    private boolean isLongClickRecord = false;
    private String from;

    public RecordAudioView(Context context) {
        super(context);
        initView();
    }

    public void setFrom(String from){
        this.from = from;
    }
    public void setLongClickRecord(boolean longClickRecord) {
        isLongClickRecord = longClickRecord;
    }

    public RecordAudioView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public RecordAudioView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        setOnClickListener(v -> {
            if (isLongClickRecord)return;
            if (v.isSelected()){
                setSelected(false);
                onFingerUp();
            }else{
                setSelected(true);
                startRecordAudio();
            }
        });
        setOnLongClickListener(v -> {
            if (isLongClickRecord){
                startRecordAudio();
            }
            return false;
        });

    }


    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if (StringUtils.equalsIgnoreCase(from, FROM_LOCKBELL)){
            return true;
        }
        if (!isClickable()||!isLongClickRecord)return true;
        if (recordAudioListener != null) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    TGLog.d("MotionEvent.ACTION_DOWN:");
                    setSelected(true);
                    break;
                case MotionEvent.ACTION_UP:
                    setSelected(false);
                    onFingerUp();
                    break;
                case MotionEvent.ACTION_CANCEL:
                    onFingerUp();
                    break;
                default:

                    break;
            }
        }
        return true;
    }

    /**
     * 手指抬起,可能是取消录制也有可能是录制成功
     */
    private void onFingerUp() {
        if (isRecording) {
            stopRecordAudio();
        }
    }


    /**
     * 检查是否ready录制,如果已经ready则开始录制
     */
    private void startRecordAudio() throws RuntimeException {
        String title =  getContext().getString(com.module.appcommon.R.string.no_permission_to_work_with_talk);
        String content =  getContext().getString(com.module.appcommon.R.string.enable_permission_mic);

        PermissionUtil permissionUtil = new PermissionUtil(getContext());
        permissionUtil.checkPermissionFirst(Manifest.permission.RECORD_AUDIO, () -> {
            TGLog.d("device_feature", "isRecording = " + isRecording + ", recordAudioListener = " + recordAudioListener);
            isRecording = true;
            if (recordAudioListener!=null){
                this.recordAudioListener.onRecordStart();
            }
        }, null, false, title, content);
    }

    /**
     * 停止录音
     */
    public void stopRecordAudio() throws RuntimeException {
        TGLog.d("device_feature", "stopRecordAudio RuntimeException isRecording" + isRecording);
        if (isRecording) {
            isRecording = false;
            TGLog.d("device_feature", "isRecording = " + isRecording + ", recordAudioListener = " + recordAudioListener);
            if (recordAudioListener!=null){
                this.recordAudioListener.onRecordStop();
            }
        }
    }

    /**
     * 需要设置IRecordAudioStatus,来监听开始录音结束录音等操作,并对权限进行处理
     */
    public void setRecordAudioListener(IRecordAudioListener recordAudioListener) {
        this.recordAudioListener = recordAudioListener;
    }



    public interface IRecordAudioListener {

        void onRecordStart();

        void onRecordStop();
    }
}